package LeetCode.leetcode113;

import java.util.ArrayList;
import java.util.List;

public class Review1 {
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        if (root == null) return res;
        backTracking(root,res,path,targetSum);
        return res;
    }
    public static void backTracking(TreeNode root, List<List<Integer>> res, List<Integer> path, int target){
        target -= root.val;
        path.add(root.val);
        if (root.left == null && root.right == null){
            if (target == 0) {
                res.add(new ArrayList<>(path));
            }
            return;
        }
        if (root.left != null){
            backTracking(root.left,res,path,target);
            path.remove(path.size() - 1);
        }
        if (root.right != null){
            backTracking(root.right,res,path,target);
            path.remove(path.size() - 1);
        }
        target += root.val;
    }
}
